﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.CMS.Process;
using TjZheng.Controls;
using TjZheng.Framework;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;

namespace TjZheng.CMS.Controllers
{
    public class ButtonInfoController : CMSController
    {
        /// <summary>
        /// 业务逻辑层
        /// </summary>
        BizButtonInfo objBizButtonInfo { get; set; }
        public ButtonInfoController()
        {
            objBizButtonInfo = new BizButtonInfo();
        }
        /// <summary>
        /// 列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 列表数据或搜索数据
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">页条数</param>
        /// <returns></returns>
        [QueryMsSqlFilter]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            //return Json(objBizButtonInfo.GetPageResult(page - 1, rows, QueryResult.Condition, "Id")); 
            return Json(objBizButtonInfo.GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters, "SortIndex asc"));
        }
        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id">数据id</param>
        /// <returns></returns>
        public ActionResult EditPage(long? id)
        {
            ButtonInfo objButtonInfo = null;
            if (id.HasValue)
            {
                objButtonInfo = objBizButtonInfo.GetRecord(id.Value);
            }
            else
            {
                objButtonInfo = new ButtonInfo();
            }
            return View(objButtonInfo);
        }
        /// <summary>
        /// 编辑页面-保存
        /// </summary>
        /// <param name="objProvide">数据源</param>
        /// <returns></returns>
        //[ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Save(ButtonInfo objProvide)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objProvide == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                objInvokeResult.ResultMessage = "保存失败";
                return Json(objInvokeResult, "text/plain");
            }
            ButtonInfo objTarget = null;
            if (objProvide.Id <= 0)
            {
                objTarget = new ButtonInfo();
                Save(objTarget, objProvide);
                objTarget.ButtonCode = Guid.NewGuid().ToString();
                objTarget.Id = objBizButtonInfo.Add(objTarget);
                objTarget.SortIndex = objTarget.Id;
                objBizButtonInfo.Update(objTarget);
            }
            else
            {
                objTarget = objBizButtonInfo.GetRecord(objProvide.Id);
                if (objTarget == null)
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "保存失败";
                    return Json(objInvokeResult, "text/plain");
                }
                Save(objTarget, objProvide);
                objBizButtonInfo.Update(objTarget);
            }

            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }
        /// <summary>
        /// 列表页面-删除
        /// </summary>
        /// <param name="id">数据id或id集合</param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult, "text/plain");
            }
            objBizButtonInfo.DeleteIDString(id);
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }

        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ButtonList()
        {
            return View(objBizButtonInfo.GetList(""));
        }

        /// <summary>
        /// 数据赋值
        /// </summary>
        /// <param name="objTarget">目标数据</param>
        /// <param name="objProvide">数据源</param>
        void Save(ButtonInfo objTarget, ButtonInfo objProvide)
        {
            objTarget.ButtonName = objProvide.ButtonName.CutWord(64);
            objTarget.IsWriteControllerName = objProvide.IsWriteControllerName;
            objTarget.DefaultActionName = objProvide.DefaultActionName.CutWord(64);
            objTarget.DefaultPosition = objProvide.DefaultPosition;
            objTarget.DefaultExecution = objProvide.DefaultExecution.CutWord(64);
            objTarget.Icon = objProvide.Icon.CutWord(64);
        }
    }
}
